package com.hxzy.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author wzm
 * @create 2024-03-06 16:43
 * project_name:JavaWeb2401
 * @Description
 */
//上传文件到阿里云oss的工具类
@Slf4j
@Component //普通组件注入的ioc容器
public class AliyunOssUtil {
    //将需要的信息全部配置称为全局变量
  /*  //地域节点
    String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    //id
    String accessKeyId = "LTAI5tFZsW3yGXSbR5U9iadA";
    //secret
    String accessKeySecret = "URx7yZ8vE2njjwE1OHuUPk5JfALRky";
    //bucket名字
    String bucketName = "web2401";*/


    //四个参数没有值  null
    //@Value注解用于外部配置文件的注入 @Value("${配置文件中的key}")
    //阿里云的节点域名
 /*   @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    //阿里云用户身份id
    @Value("${aliyun.oss.accesskeyId}")
    private String accesskeyId;
    //阿里云秘钥
    @Value("${aliyun.oss.accesskeySecret}")
    private String accesskeySecret;
    //存储空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;*/

    //获取阿里云ossbean对象 然后从对象的属性里获取
    @Autowired
    private AliyunOssBean aliyunOssBean; //此对象包含了从yml中获取阿里云oss的配置信息


    //实现上传
    public String putOss(MultipartFile multipartFile) throws IOException {
        //1.代码嵌入java凭证
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliyunOssBean.getAccesskeyId(), aliyunOssBean.getAccesskeySecret());

        //2.获取文件 读取文件流
        InputStream inputStream = multipartFile.getInputStream();

        //定义上传后的文件名字是什么
        //UUID
        String uuid = UUID.randomUUID().toString();
        //原始文件名
        String originalFilename = multipartFile.getOriginalFilename();

        //后缀名 = 原始文件名的最后一个点后面的所有内容
        String houzhui = originalFilename.substring(originalFilename.lastIndexOf("."));

        //3.文件名=uuid+后缀
        String newFileName = uuid + houzhui;

        //4.上传文件
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliyunOssBean.getEndpoint(), credentialsProvider);

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOssBean.getBucketName(), newFileName, inputStream);

        // 创建PutObject请求。 上传文件
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        log.info("上传文件的结果{}", result);


        //5.图片的url?
        //url= https://+bucketname+.+域名节点//斜杠分割2部分+/+文件名
        //https  域名节点//斜杠分割1部分
        //https://web2401.oss-cn-beijing.aliyuncs.com/11000.jpg
        String url = aliyunOssBean.getEndpoint().split("//")[0] + aliyunOssBean.getBucketName() + "." + aliyunOssBean.getEndpoint().split("//")[1] + "/" + newFileName;

        //6.关闭客户端
        if (ossClient != null) {
            ossClient.shutdown();
        }

        //7.返回文件地址
        return url;
    }

}
